<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends CommonController{
    public function showList(){
        //查询数据
        $model = M('Category');
        $data = $model -> order('id desc') -> select();
        $this -> assign('data',$data);
        $this -> display();
    }

    public function add(){
        
        if(IS_POST){
            //处理提交数据
            //实例化模型类
            $model = D('Category');
            $data = $model -> create();
            //dump($data);die;

            if(!$data){
                //提标用户验证失败
                //echo $model -> getError();die;
                //dump($model -> getError());die;
                $this -> error($model -> getError());exit;
            }

            $data['addtime'] = time();            
            //dump($data);die;

            $result = $model -> add($data);
            if($result){
                $this -> success('添加成功！',U('showList'));
                //$this -> redirect('Admin/Category/showList');
            }else{
                $this -> error('添加失败！');
            }
        }else{
            $this -> display();
        }
    }

    public function edit(){
        if(IS_POST){
            //实例化模型类
            $model = D('Category');
            $data = $model -> create();
            //dump($data);die;

            if(!$data){
                //提标用户验证失败
                //echo $model -> getError();die;
                //dump($model -> getError());die;
                $this -> error($model -> getError());exit;
            }

            $data['addtime'] = time();            
            //dump($data);die;

            $result = $model -> save($data);
            if($result){
                $this -> success('修改成功！',U('showList'));
                //$this -> redirect('Admin/Category/showList');
            }else{
                $this -> error('修改失败！');
            }
        }else{
            //接收id
            $id = I('get.id');
            //实例化模型
            $model = M('Category');
            //查询类别信息
            $data = $model -> find($id);
            $this -> assign('data',$data);
            //展示模板
            $this -> display();
        }
    }

    public function del(){
        if(IS_POST){
            //post删除
            //接收参数 
            $id = I('post.id');
            //模型实例化
            $model = M('Category');
            //删除
            $result = $model -> delete($id);
            if($result){
                //成功
				//删除对应栏目的数据
                $data_model = M('Data');
                $data_model->where("category_id=$id")->delete();
                $this -> success('删除成功！',U('showList'));
            }else{
                $this -> error('删除失败！');
            }
        }else{
            //接收id
            $id = I('get.id');
            //实例化模型
            $model = M('Category');
            //查询类别信息
            $data = $model -> find($id);
            $this -> assign('data',$data);
            //展示模板
            $this -> display();
        }
    }
}